from flask import Blueprint, request

from app.api.v1.controller.base import Base
from app.api.v1.service.image_service import ImageService
from zhanshop.app import App
from zhanshop.validate import Validate

v1_image = Blueprint("v1_image", __name__, url_prefix='/v1/image')
class Image(Base):
    @v1_image.route('/repository', methods=["GET"])
    @staticmethod
    def getRepository():
        """
        @api GET repository 图片管理
        @apiParam string database 数据库名
        @apiParam string table 知识库表名
        @apiParam json model= 模型参数更多参数参考：chromadb->https://docs.trychroma.com/docs/overview/getting-started,SentenceTransformer->https://sbert.net/docs/sentence_transformer/usage/usage.html
        @apiParam int limit 结果最大返回数
        @apiParam url file 需要检索的图像
        @apiSuccess string data 搜索后的结果
        @apiGroup 向量库
        @apiDescription 搜索一个图片库
        @return:
        """
        filePath = Image.getUploadFile(request)
        inputData = Validate(request, {
            "database": "required | string",
            "table": "required | string",
            "limit": "required | int",
            "model": "json"
        }).getData()
        result = App.make(ImageService).getRepository(inputData["database"], inputData["table"], filePath, inputData["limit"], inputData["model"])
        return Image.result(result)

    @v1_image.route('/repository', methods=["POST"])
    @staticmethod
    def postRepository():
        """
        @api POST repository 添加图片库
        @apiParam string database 数据库名
        @apiParam string table 知识库表名
        @apiParam json model= 模型参数更多参数参考：chromadb->https://docs.trychroma.com/docs/overview/getting-started,SentenceTransformer->https://sbert.net/docs/sentence_transformer/usage/usage.html
        @apiParam json metadata= 图像相关源数据
        @apiParam url file 需要添加的图片
        @apiSuccess string data 添加后的结果
        @apiGroup 向量库
        @apiDescription 将一个图像添加到图像检索库
        @return:
        """
        filePath = Image.getUploadFile(request)
        inputData = Validate(request, {
            "database": "required | string",
            "table": "required | string",
            "metadata": "required | json",
            "model": "json"
        }).getData()
        result = App.make(ImageService).postRepository(inputData["database"], inputData["table"], [inputData["metadata"]], inputData["model"])
        return Image.result(result)
